@extends ('layout')
@section('content')
<div class="panel panel-info">
      <div class="panel-heading">
      <h3><left><a href = "#"><a class="glyphicon glyphicon-shopping-cart text-info"></a></left>Agregar Nueva compra</h3>
      </div>
    </div>
     <style>     
          body {      
             width: 1300px;      
             margin: 50px auto;      
         }               
      </style>
<body>
  {{Form::open(array('url' => 'compras', 'method' => 'post'))}}
    <div class="row">
            <div class="col-sm-6 col-md-6 col-lg-6">
       {{ Form::label('fecha', 'Fecha:') }}
        <p>{{Form::input('date', 'fecha', null, array('class' => 'form-control', 'type' => 'Date', 'required' => 'required'))}}</p>
           </div>
        <div class="col-sm-6 col-md-6 col-lg-6">
        {{ Form::label('num_factura', 'Nº Factura:') }}
        <p>{{Form::text('num_factura', null, array('class' => 'form-control', 'placeholder' => 'Nº de factura', 'id' => 'num_factura', 'required' => 'required'))}}</p>
        </div>
        <div class="col-sm-6 col-md-6 col-lg-6">
          {{ Form::label('id_proveedors', 'Proveedor') }}
          <p>{{ Form::select('proveedor', ['' => 'Seleccione un proveedor'] + $client_options , Input::old('proveedor'), array('id' => 'proveedor', 'class' => 'form-control')) }}</p>
        </div>
        <div class="col-sm-6 col-md-6 col-lg-6">
        	{{ Form::label('nombre_vendedor', 'Persona de Contacto:') }}
        <p>{{Form::text('nombre_vendedor', null, array('class' => 'form-control', 'placeholder' => 'Persona de Contacto', 'required' => 'required'))}}</p>
        </div>
	</div>
						<label class="btn btn-primary btn-sm">
							<input type="checkbox" name="casilla" id="casilla" value="1" style="display:none;" /><span class="glyphicon glyphicon-plus"></span> Agregar Proveedor
						</label> 
      <div class="row">
			<div class="col-sm-12 col-md-12 col-lg-12" >
					<div class="agregarproveedor form-inline" id="agregarproveedor" name="agregarproveedor" style="display:none;">
							<input type="text" name="id_proveedor" class="form-control form-group" placeholder="Nombre del Proveedor">
							<input type="text" name="ruc" class="form-control form-group" placeholder="RUC">
							<input type="text" name="telef" class="form-control form-group" placeholder="Teléfono">
					</div>
			</div>
	  </div>
	  
    
    <div class="row">
        <div class="col-sm-12">
            <legend>Compras:</legend>
        </div>
        <!-- panel preview -->
        <div class="col-sm-5">
            <h4>Agregar pago:</h4>
            <div class="panel panel-default">
                <div class="panel-body form-horizontal payment-form">
                    <div class="form-group">
                        <label for="sexo" class="col-sm-3 control-label">Sexo</label>
                        <div class="col-sm-9">
                            {{ Form::select('sexo', ['' => '', '0' => 'Hembra', '1' => 'Macho'],Input::old('sexo'), array('id' => 'sexo', 'class' => 'form-control', 'style' => 'min-width:100px;')) }}
                        </div>
                    </div>
                    <div class="form-group">
                        <label for="color" class="col-sm-3 control-label">Color</label>
                        <div class="col-sm-9">
                            <input class="form-control" id="color" name="color" type="text" placeholder="Color">
                        </div>
                    </div> 
                    <div class="form-group">
                        <label for="raza" class="col-sm-3 control-label">Raza</label>
                        <div class="col-sm-9">
                            {{ Form::select('raza', ['' => ''] + $razas , Input::old('razas'), array('id' => 'raza', 'class' => 'form-control', 'style' => 'min-width:100px;')) }}
                        </div>
                    </div>
                    <div class="form-group">
                        <label for="tipo" class="col-sm-3 control-label">Tipo</label>
                        <div class="col-sm-9">
                            {{ Form::select('tipo',  ['' => ''] + $tipos_ganados , Input::old('tipos_ganados'), array('id' => 'tipo', 'class' => 'form-control', 'style' => 'min-width:100px;')) }}
                        </div>
                    </div>
                    <div class="form-group">
                        <label for="precio" class="col-sm-3 control-label">Precio x kg</label>
                        <div class="col-sm-9">
                            <input class="form-control" id="precio" name="precio" type="text" placeholder="$">
                        </div>
                    </div>
                  <div class="form-group">
                        <label for="peso" class="col-sm-3 control-label">Peso</label>
                        <div class="col-sm-9">
                            <input class="form-control" id="peso" name="peso" type="text" placeholder="kg">
                        </div>
                    </div>  
                    <div class="form-group">
                        <div class="col-sm-12 text-right">
                            <button type="button" id="limpiar" class="btn btn-default">Limpiar</button>
                            <button type="button" class="btn btn-default preview-add-button">
                                <span class="glyphicon glyphicon-plus"></span> Agregar
                            </button>
                        </div>
                    </div>
                </div>
            </div>           
        </div> <!-- / panel preview -->
        <div class="col-sm-7">
            <h4>Estado:</h4>
            <div class="row">
                <div class="col-xs-12">
                    <div class="table-responsive">
                        <table class="table preview-table">
                            <thead>
                                <tr>
                                    <th>Sexo</th>
                                    <th>Color</th>
                                    <th>Raza</th>
                                    <th>Tipo</th>
                                    <th>Precio x kg</th>
                                    <th>Peso</th>
                                    <th>Monto</th>
                                </tr>
                            </thead>
                            <tbody></tbody> <!-- preview content goes here-->
                        </table>
                    </div>                            
                </div>
            </div>

            <div class="row">
                <div class="col-xs-4">
                    <label for="total">Total:</label>
                    <input type="number" name="total_bruto" id="total_bruto" class="form-control preview-total" placeholder="$" readonly>
                </div>
            </div>
            <div class="row">
                <div class="col-xs-4">
                    <label for="total">subtotal:</label>
                    <input type="number" name="total" id="total" class="form-control preview-subtotal" placeholder="$" readonly>
                </div>
            </div>
            <div class="row">
                <div class="col-xs-4">
                    <label for="desc">Descuento:</label>
                    <input type="number" name="desc" id="desc" class="form-control desc" placeholder="%">
                </div>                
            </div>
            <div class="row">
                <div class="col-xs-4">
                    <label for="desc">ITBMS:</label>
                    <input type="number" name="itbms" id="itbms" class="form-control itbms" placeholder="%">
                </div>                
            </div>
        </div>
    </div>



      <div class="modal-footer">
      	<button type="submit" class="btn btn-warning" style="width: 30%"><span class="glyphicon glyphicon-ok-sign"></span> Finalizar Compra</button>
               <a href="{{URL::to('compras')}}"><button type="button" class="btn btn-default" style="width: 30%" data-dismiss="modal"><span ></span> Regresar</button></a>
		{{Form::close()}}
      </div>	


	@if(Session::has('message'))
		<div class="alert alert-{{ Session::get('class') }}">{{ Session::get('message')}}</div>
	@endif
	<script type="text/javascript">
    $(document).ready(function(){
        $('input[type="checkbox"]').click(function(){
            if ($(this).is(':checked')) {
            	$('#proveedor').attr('disabled', true);
                $(".agregarproveedor").toggle("slow");
            }
           	else{
				$('#proveedor').removeAttr('disabled');
				$(".agregarproveedor").toggle("slow");
			}
        });
    function calc_total(){ //Calcula el monto total de cada campo
    var sum = 0;
    $('.input-imonto').each(function(){
        sum += parseFloat($(this).text());
    });
    
    $(".preview-total").val(sum.toFixed(2)); 
    $(".preview-subtotal").val(sum.toFixed(2));   
}
$(document).on('click', '.input-remove-row', function(){ 
    var tr = $(this).closest('tr');
    tr.fadeOut(200, function(){
        tr.remove();
        calc_total()
    });
});

    var x = 1;
    var form_data = {};
    $('.preview-add-button').click(function( e ){
        e.preventDefault();//this line prevents the form from submitting
        form_data["isexo"] = $('.payment-form #sexo option:selected').text();
        form_data["icolor"] = $('.payment-form input[name="color"]').val();
        form_data["iraza"] = $('.payment-form #raza option:selected').text();
        form_data["itipo"] = $('.payment-form #tipo option:selected').text();
        form_data["iprecio"] = $('.payment-form input[name="precio"]').val();
        form_data["ipeso"] = $('.payment-form input[name="peso"]').val();
        form_data["imonto"] = parseFloat($('.payment-form input[name="precio"]').val()*$('.payment-form input[name="peso"]').val()).toFixed(2);
        form_data["remove-row"] = '<span class="glyphicon glyphicon-remove"></span>';
        var row = $('<tr></tr>');
        $.each(form_data, function( type, value ) {
            $('<td class="input-'+type+'">'+value+'<input type="text" style="display:none" class="form-control" name="data['+x+']['+type+']" value="'+value+'"></td>').val(value).appendTo(row);
        });
        $('.preview-table > tbody:last').append(row); 
        calc_total();
        x++;
        $('#sexo').val('');
        $('#color').val('');
        $('#raza').val('');
        $('#tipo').val('');
        $('#precio').val('');
        $('#peso').val('');
        console.log( form_data );
    }); 

 $('.desc').change(function(){   //Calculando el descuento si el input descuento esta vacio
        var a = $('.preview-total').val();
            if($(this).val() != '')
            {
                var val = $('.desc').val();
                var desc = (parseFloat(a) -  (parseFloat(a)* val)/100);
                $(".preview-subtotal").val(desc.toFixed(2));
            }
            else
            {
               calc_total()
            }
         });
  $('.itbms').change(function(){   //Calculando el descuento si el input descuento esta vacio
        var a = $('.preview-subtotal').val();
            if($(this).val() != '')
            {
                var val = $('.itbms').val();
                var desc = (parseFloat(a) +  (parseFloat(a)* val)/100);
                $(".preview-subtotal").val(desc.toFixed(2));
            }
            else
            {
               $('.desc').change();
            }
         });
});
</script>
@stop